package VIEWS;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.border.EmptyBorder;

import CONTROLLERS.UsersControllers;
import ModelEntityFW.UserEntitys;

public class frmUserAdd extends JFrame {

    private final JPanel contentPane;
    private final JTextField txtID;
    private final JTextField txtUserName;
    private final JTextField txtFullName;
    private final JTextField txtBirthday;
    private final JComboBox cboSex;
    private final JTextField txtMobile;
    private final JTextField txtEmail;
    private final JTextField txtAddress;

    private static Connection m_objConnection;
    private static UserEntitys m_objSecurityContext;
    private static UserEntitys m_objUserEntity;
    private UsersControllers m_objUserController;
    private final JPasswordField txtPassword;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    frmUserAdd frame = new frmUserAdd(m_objConnection, m_objSecurityContext);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    /**
     * Create the frame.
     */
    public frmUserAdd(Connection objConnection, UserEntitys objSecurity) {
        setTitle("Thông tin chi tiết");
        m_objConnection = objConnection;
        m_objSecurityContext = objSecurity;
        m_objUserEntity = new UserEntitys();
        m_objUserEntity = objSecurity;
        m_objUserController = new UsersControllers(m_objConnection, m_objSecurityContext);

        setResizable(false);
        setIconImage(Toolkit.getDefaultToolkit().getImage(frmUserAdd.class.getResource("/images/contacts.png")));
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setBounds(100, 100, 417, 420);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JToolBar toolBar = new JToolBar();
        toolBar.setBounds(0, 0, 401, 38);
        contentPane.add(toolBar);

        JButton btnCapNhat = new JButton("Đăng kí ");
        btnCapNhat.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // Kiểm tra trạng thái của Entity
                if (m_objUserEntity == null) {
                    m_objUserEntity = new UserEntitys();
                }
                // 3. Thiết lập thông tin cho Entity
                m_objUserEntity.setUserId(txtID.getText());
                m_objUserEntity.setUserName(txtUserName.getText());
                m_objUserEntity.setPasswords(txtPassword.getText());
                m_objUserEntity.setFullName(txtFullName.getText());
                m_objUserEntity.setBirthday(txtBirthday.getText());
                if (cboSex.getSelectedIndex() == 0) {
                    m_objUserEntity.setSex(true);
                } else {
                    m_objUserEntity.setSex(false);
                }
                m_objUserEntity.setMobile(txtMobile.getText());
                m_objUserEntity.setEmail(txtEmail.getText());
                m_objUserEntity.setAddress(txtAddress.getText());

                // 4. Truy�?n thông tin cho UserController xử lý
                if (m_objUserController == null) {
                    m_objUserController = new UsersControllers(m_objConnection, m_objSecurityContext);
                }

                // Thực thi Thêm hoặc Cập Nhật
                if (m_objUserEntity.isbFlag()) {
                    boolean bResult = false;
                    try {
                        bResult = m_objUserController.ExecOfUpdateElement(m_objUserEntity);
                    } catch (Exception e) {
                        JOptionPane.showMessageDialog(getParent(), "Err: " + e, "Thông báo",
                                JOptionPane.INFORMATION_MESSAGE);
                    }
                    if (bResult) {
                        JOptionPane.showMessageDialog(getParent(), "Cập nhật tài khoản thành công!", "Thông báo",
                                JOptionPane.INFORMATION_MESSAGE);

                        frmUsers fU = new frmUsers(m_objConnection, m_objSecurityContext);
                        fU.setVisible(true);
                        dispose();
                    }
                } else {
                    boolean bResult = false;
                    try {
                        bResult = m_objUserController.ExecOfInsertElement(m_objUserEntity);
                    } catch (Exception e) {
                        JOptionPane.showMessageDialog(getParent(), "Err: " + e, "Thông báo",
                                JOptionPane.INFORMATION_MESSAGE);
                    }
                    if (bResult) {
                        JOptionPane.showMessageDialog(getParent(), "Thêm tài khoản thành công!", "Thông báo",
                                JOptionPane.INFORMATION_MESSAGE);

                        frmUsers fU = new frmUsers(m_objConnection, m_objSecurityContext);
                        fU.setVisible(true);
                        // Giải phóng form
                        dispose();
                    }
                }
            }
        });
        btnCapNhat.setIcon(new ImageIcon(frmUserAdd.class.getResource("/images/book_edit.png")));
        btnCapNhat.setFont(new Font("Cambria", Font.PLAIN, 14));
        toolBar.add(btnCapNhat);

        JButton btnTrogiup = new JButton("Trợ Giúp");
        btnTrogiup.setIcon(new ImageIcon(frmUserAdd.class.getResource("/images/edoc_register.png")));
        btnTrogiup.setFont(new Font("Cambria", Font.PLAIN, 14));
        toolBar.add(btnTrogiup);

        JButton btnDong = new JButton("Đóng");
        btnDong.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                dispose();
            }
        });
        btnDong.setIcon(new ImageIcon(frmUserAdd.class.getResource("/images/cancel.gif")));
        btnDong.setFont(new Font("Cambria", Font.PLAIN, 14));
        toolBar.add(btnDong);

        JLabel lblNewLabel = new JLabel("ID:");
        lblNewLabel.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblNewLabel.setBounds(20, 49, 57, 14);
        contentPane.add(lblNewLabel);

        txtID = new JTextField();
        txtID.setText("0");
        txtID.setFont(new Font("Cambria", Font.PLAIN, 14));
        txtID.setBounds(99, 41, 104, 25);
        contentPane.add(txtID);
        txtID.setColumns(10);

        // Thiết lập thuộc tính cho TextField
        // txtID.setEditable(false);

        JLabel lblUsername = new JLabel("UserName:");
        lblUsername.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblUsername.setBounds(20, 80, 70, 14);
        contentPane.add(lblUsername);

        JLabel lblPassword = new JLabel("Password:");
        lblPassword.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblPassword.setBounds(20, 112, 82, 14);
        contentPane.add(lblPassword);

        JLabel lblFullname = new JLabel("FullName:");
        lblFullname.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblFullname.setBounds(20, 149, 82, 14);
        contentPane.add(lblFullname);

        JLabel lblBirthday = new JLabel("Birthday:");
        lblBirthday.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblBirthday.setBounds(20, 185, 82, 14);
        contentPane.add(lblBirthday);

        txtUserName = new JTextField();
        txtUserName.setFont(new Font("Cambria", Font.PLAIN, 14));
        txtUserName.setColumns(10);
        txtUserName.setBounds(99, 72, 292, 25);
        contentPane.add(txtUserName);

        txtFullName = new JTextField();
        txtFullName.setFont(new Font("Cambria", Font.PLAIN, 14));
        txtFullName.setColumns(10);
        txtFullName.setBounds(99, 144, 292, 25);
        contentPane.add(txtFullName);

        txtBirthday = new JTextField();
        txtBirthday.setFont(new Font("Cambria", Font.PLAIN, 14));
        txtBirthday.setColumns(10);
        txtBirthday.setBounds(99, 180, 213, 25);
        contentPane.add(txtBirthday);

        JButton btnGetDate = new JButton("Get Date");
        btnGetDate.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {

            }
        });
        btnGetDate.setBounds(322, 180, 70, 25);
        contentPane.add(btnGetDate);

        JLabel lblSex = new JLabel("Sex:");
        lblSex.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblSex.setBounds(19, 220, 82, 14);
        contentPane.add(lblSex);

        txtMobile = new JTextField();
        txtMobile.setFont(new Font("Cambria", Font.PLAIN, 14));
        txtMobile.setColumns(10);
        txtMobile.setBounds(97, 246, 293, 25);
        contentPane.add(txtMobile);

        JLabel lblMobile = new JLabel("Mobile:");
        lblMobile.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblMobile.setBounds(18, 251, 82, 14);
        contentPane.add(lblMobile);

        JLabel lblEmail = new JLabel("Email:");
        lblEmail.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblEmail.setBounds(19, 285, 82, 14);
        contentPane.add(lblEmail);

        txtEmail = new JTextField();
        txtEmail.setFont(new Font("Cambria", Font.PLAIN, 14));
        txtEmail.setColumns(10);
        txtEmail.setBounds(98, 280, 293, 25);
        contentPane.add(txtEmail);

        JLabel lblAddress = new JLabel("Address:");
        lblAddress.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblAddress.setBounds(18, 319, 82, 14);
        contentPane.add(lblAddress);

        txtAddress = new JTextField();
        txtAddress.setFont(new Font("Cambria", Font.PLAIN, 14));
        txtAddress.setColumns(10);
        txtAddress.setBounds(97, 314, 293, 57);
        contentPane.add(txtAddress);

        cboSex = new JComboBox();
        cboSex.setModel(new DefaultComboBoxModel(new String[] { "Nam", "Nữ" }));
        cboSex.setBounds(98, 214, 70, 27);
        contentPane.add(cboSex);

        txtPassword = new JPasswordField();
        txtPassword.setBounds(99, 108, 292, 25);
        contentPane.add(txtPassword);

        if (m_objUserEntity.isbFlag()) {
            // Khi cập nhật
            txtID.setEnabled(false);
            BindingControls(m_objUserEntity);
        } else {
            // Khi thêm
            txtID.setEnabled(false);
            txtID.setText(objSecurity.getUserId());
        }
    }

    void BindingControls(UserEntitys objUserEntity) {
        txtID.setText("" + objUserEntity.getUserId());
        txtUserName.setText(objUserEntity.getUserName());
        txtPassword.setText(objUserEntity.getPasswords());
        txtFullName.setText(objUserEntity.getFullName());
        txtBirthday.setText(objUserEntity.getBirthday());
        if (objUserEntity.isSex()) {
            cboSex.setSelectedIndex(0);
        } else {
            cboSex.setSelectedIndex(1);
        }
        txtMobile.setText(objUserEntity.getMobile());
        txtEmail.setText(objUserEntity.getEmail());
        txtAddress.setText(objUserEntity.getAddress());
    }
}
